//https://leetcode.cn/problems/minimum-operations-to-reduce-x-to-zero/

class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        //找和为 数组和-x后剩下的值 的最长子序列
        int left = 0, right = 0, n = nums.size(), len = -1;
        int sum = 0;
        int num = 0;
        for (auto e : nums)
        {
            sum += e;
        }
        int target = sum - x;
        if (target < 0)
            return-1;

        while (right < n)
        {
            //进窗口
            num += nums[right];
            //判断
            while (num > target)
            {
                //出窗口
                num -= nums[left++];
            }
            //更新结果
            if (num == target)
                len = max(len, right - left + 1);

            right++;

        }
        if (len == -1)
            return -1;

        return n - len;

    }
};